package com.diana.common;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.StorageClass;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Desc 腾讯云cos对象存储
 */
public class TxCosUtils {

    private static String bucketName = "anzhi-1259528483"; //桶的名称
    private static String region = "ap-beijing";//区域
    private static String appId = "1259528483"; //APPID
    private static COSCredentials cred = null;
    private static TransferManager transferManager = null;
    private static ClientConfig clientConfig = null;
    private static String URL = "https://anzhi-1259528483.cos.ap-beijing.myqcloud.com";

    private static Logger log = LoggerFactory.getLogger(TxCosUtils.class);

    static {
        // 1 初始化用户身份信息(secretId, secretKey)
        //SecretId 是用于标识 API 调用者的身份
        String SecretId = "AKIDSkx84ldUIrAE4es23pMdeRyFNJ1TGcOu";
        //SecretKey是用于加密签名字符串和服务器端验证签名字符串的密钥
        String SecretKey = "PYT8XgeNr59zazILCzbw4xa0BCyMXUDi";
        cred = new BasicCOSCredentials(SecretId, SecretKey);
        // 2 设置bucket的区域,
        clientConfig = new ClientConfig(new Region(region));
    }

    /**
     * @param path  上传路径
     * @param file  上传文件
     * @return String
     * @Des:  将本地文件上传到COS
     */
    public static String upload(String path ,File file) {
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        String key = path+file.getName();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
        putObjectRequest.setStorageClass(StorageClass.Standard);
        try {
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            // 关闭客户端
            cosClient.shutdown();
            return URL+key;
        } catch (CosServiceException e) {
            e.printStackTrace();
            log.error("上传COS服务器异常." + e.getMessage(), e);
        } catch (CosClientException e) {
            e.printStackTrace();
            log.error("上传COS服务器异常." + e.getMessage(), e);
        }
        // 关闭客户端
        cosClient.shutdown();
        return null;
    }

    /**
     * @param path  上传路径
     * @param input  文件流
     * @param fileName  文件名
     * @return String
     * @Des:  将本地文件上传到COS
     */
    public static String upload(String path,InputStream input,String fileName) throws IOException {
        //  生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据，存在内存OOM的情况
        objectMetadata.setContentLength(Integer.valueOf(input.available()).longValue());
        // 默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
        //objectMetadata.setContentType(getContentType(fileName));
        String key = path+fileName;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata);
        // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
        putObjectRequest.setStorageClass(StorageClass.Standard);
        try {
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            // 关闭客户端
            cosClient.shutdown();
            return URL+key;
        } catch (CosServiceException e) {
            e.printStackTrace();
            log.error("上传COS服务器异常." + e.getMessage(), e);
        } catch (CosClientException e) {
            e.printStackTrace();
            log.error("上传COS服务器异常." + e.getMessage(), e);
        }
        // 关闭客户端
        cosClient.shutdown();
        return null;
    }

}